package com.viphuli.app.tool.fragment;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import butterknife.Bind;
import butterknife.OnClick;

import com.loopj.android.http.RequestParams;
import com.viphuli.app.tool.R;
import com.viphuli.app.tool.bean.part.HolidayHomeLeave;
import com.viphuli.app.tool.common.AccessTokenKeeper;
import com.viphuli.app.tool.common.MyPageHelper;
import com.viphuli.app.tool.common.PerferencesHelper;
import com.viphuli.app.tool.http.ApiRequest;

/**
 * 假期管理
 * 
 * @author li.li
 *
 */
public class HolidayFragment extends BaseProgressFragment {

	public static final String PREF_SAVE_SHOW_SHOW_SWITCH = "pref_save_holiday_show_switch";

	@Bind(R.id.id_holiday_limit_setting)
	protected View limitSettingView;

	@Bind(R.id.id_holiday_work_record)
	protected View workRecordView;

	@Bind(R.id.id_holiday_leave_record)
	protected View leaveRecordLeave;

	@Bind(R.id.id_holiday_switch)
	protected TextView holidaySwitch;

	@Bind(R.id.id_holiday_layout)
	protected View holidayLayout;

	@Bind(R.id.id_listview)
	protected ListView listview;

	private BaseAdapter adapter;

	private List<HolidayHomeLeave> list = new ArrayList<HolidayHomeLeave>();

	@Override
	protected int obtainContentRes() {
		return R.layout.frag_holiday;
	}

	@Override
	protected void initParams() {

	}

	@Override
	protected void initView() {

	}

	@Override
	protected void initData() {

		boolean isSwitch = PerferencesHelper.getInstance().readBoolean(PREF_SAVE_SHOW_SHOW_SWITCH, true);

		if (isSwitch) {

			holidaySwitch.setBackgroundResource(R.drawable.ic_switch_on);
			holidayLayout.setVisibility(View.VISIBLE);
			listview.setVisibility(View.VISIBLE);

		} else {

			holidaySwitch.setBackgroundResource(R.drawable.ic_switch_off);
			holidayLayout.setVisibility(View.GONE);
			listview.setVisibility(View.GONE);

		}

		AccessTokenKeeper tokenKeeper = AccessTokenKeeper.readAccessToken(getActivity());
		if (tokenKeeper.isLogin()) {

			RequestParams params = new RequestParams();
			params.put("uid", tokenKeeper.getUser().getUid());
			ApiRequest.holidayHome.request(this, params, false);

		}

	}

	@OnClick(R.id.id_holiday_limit_setting)
	public void clickSetting() {

		HolidayLeaveSettingFragment.go(getActivity());

	}

	@OnClick(R.id.id_holiday_work_record)
	public void clickWorkRecord() {

		HolidayOverWorkAddFragment.go(getActivity());

	}

	@OnClick(R.id.id_holiday_leave_record)
	public void clickLeaveRecord() {

		HolidayLeaveAddFragment.go(getActivity());

	}

	@OnClick(R.id.id_holiday_switch)
	public void clickSwitch(View v) {

		boolean isSwitch = PerferencesHelper.getInstance().readBoolean(PREF_SAVE_SHOW_SHOW_SWITCH, true);

		if (isSwitch) {

			holidaySwitch.setBackgroundResource(R.drawable.ic_switch_off);
			holidayLayout.setVisibility(View.GONE);

			//持久化
			PerferencesHelper.getInstance().saveBoolean(PREF_SAVE_SHOW_SHOW_SWITCH, false);

		} else {

			holidaySwitch.setBackgroundResource(R.drawable.ic_switch_on);
			holidayLayout.setVisibility(View.VISIBLE);

			//持久化
			PerferencesHelper.getInstance().saveBoolean(PREF_SAVE_SHOW_SHOW_SWITCH, true);

		}
	}

	public ListView getListview() {
		return listview;
	}

	public void setAdapter(BaseAdapter adapter) {
		this.adapter = adapter;
	}

	public List<HolidayHomeLeave> getList() {
		return list;
	}

	public static void go(Activity act) {

		Bundle args = new Bundle();
		MyPageHelper.holidayFragment.showMyPage(act, args);

	}

}
